<template>
  <div>
    <div class="container mx-auto">
      <!-- 用户搜索表单 -->
      <el-form
        ref="formRef"
        :inline="true"
        :model="userSearch"
        class="search-form bg-bg_color px-8 pt-[12px]"
      >
        <el-form-item label="模糊搜素" prop="key">
          <el-input
            v-model="userSearch.key"
            title="请输入用户名/邮箱/手机号/单位名称/课题组名称"
            placeholder="请输入内容"
            clearable
            class="!w-[220px]"
          />
        </el-form-item>
        <el-form-item label="手机号码：" prop="phone">
          <el-input
            v-model="userSearch.phone"
            placeholder="请输入手机号码"
            clearable
            class="!w-[180px]"
          />
        </el-form-item>
        <el-form-item label="单位名称：" prop="unitname">
          <el-input
            v-model="userSearch.unitname"
            placeholder="请输入单位名称"
            clearable
            class="!w-[230px]"
          />
        </el-form-item>
        <el-form-item label="课题组名称" prop="rpgname">
          <el-input
            v-model="userSearch.rpgname"
            placeholder="请输入单位名称"
            clearable
            class="!w-[230px]"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon('ri:search-line')"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 搜索结果展示表格 -->
      <!-- :columns="columns" 定义展示列 -->
      <PureTableBar title="用户管理" :columns="columns" @refresh="onSearch">
        <!-- 表头右侧按钮 -->
        <template #buttons>
          <el-button
            type="primary"
            :icon="useRenderIcon(AddFill)"
            @click="openDialog()"
          >
            新增用户
          </el-button>
        </template>

        <!-- dynamicColumns 动态列 -->
        <template v-slot="{ size, dynamicColumns }">
          <!-- 批量操作【默认隐藏】 -->
          <div
            v-if="selectedNum > 0"
            v-motion-fade
            class="bg-[var(--el-fill-color-light)] w-full h-[46px] mb-2 pl-4 flex items-center"
          >
            <!-- 多选数量提示 -->
            <div class="flex-auto">
              <span
                style="font-size: var(--el-font-size-base)"
                class="text-[rgba(42,46,54,0.5)] dark:text-[rgba(220,220,242,0.5)]"
              >
                已选 {{ selectedNum }} 项
              </span>
              <!-- @click="" -->
              <el-button type="primary" text @click="onSelectionCancel">
                取消选择
              </el-button>
            </div>
            <!-- @confirm="onbatchDel" -->
            <!-- 批量删除提示 -->
            <el-popconfirm title="是否确认删除?" @confirm="onbatchDel">
              <template #reference>
                <el-button type="danger" text class="mr-1">
                  批量删除
                </el-button>
              </template>
            </el-popconfirm>
          </div>

          <!-- 表格内容区域 -->
          <pure-table
            ref="tableRef"
            row-key="id"
            :loading="loading"
            adaptive
            :size="size"
            :adaptiveConfig="{ offsetBottom: 108 }"
            align-whole="center"
            table-layout="auto"
            :paginationSmall="size === 'small' ? true : false"
            :pagination="pagination"
            :data="dataList"
            :columns="dynamicColumns"
            :header-cell-style="{
              background: 'var(--el-fill-color-light)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
            @page-size-change="handleSizeChange"
            @page-current-change="handleCurrentChange"
          >
            <!-- 【操作】表格按钮操作区域 -->
            <template #operation="{ row }">
              <!-- 【操作】 查看按钮 -->
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(View)"
                @click="openDialog('查看', row)"
              >
                查看
              </el-button>
              <!-- 【操作】 修改按钮 -->
              <el-button
                class="reset-margin"
                link
                type="primary"
                :size="size"
                :icon="useRenderIcon(EditPen)"
                @click="openDialog('修改', row)"
              >
                修改
              </el-button>
              <!-- 【操作】更多操作 -->
              <el-dropdown>
                <el-button
                  class="ml-3 mt-[2px]"
                  link
                  type="primary"
                  :size="size"
                  :icon="useRenderIcon(More)"
                />
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item>
                      <el-button
                        :class="buttonClass"
                        link
                        type="primary"
                        :size="size"
                        :icon="useRenderIcon(Password)"
                        @click="handleReset(row)"
                      >
                        重置密码
                      </el-button>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>

<script setup>
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { status } from "nprogress";
import { ref, reactive } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useUser } from "./utils/hook.tsx";

import Password from "@iconify-icons/ri/lock-password-line";
import More from "@iconify-icons/ep/more-filled";
import View from "@iconify-icons/ep/view";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

/** 搜索区DOM */
const formRef = ref(null);
/** 表格DOM */
const tableRef = ref();

const {
  loading,
  columns,
  dataList,
  pagination,
  userSearch,
  selectedNum,
  buttonClass,
  resetForm,
  onSearch,
  openDialog,
  handleSelectionChange,
  onSelectionCancel,
  handleReset,
  onbatchDel,
  handleSizeChange,
  handleCurrentChange
} = useUser(tableRef);
</script>

<style scoped>
/* 防止宽度不够后输入框堆叠 */
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
